package com.cltech.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
/**
 * Created by Administrator on 2018/8/19.
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * 添加摘要信息(Docket)
     */
    @Bean
    public Docket apiDocument() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("api")
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cltech.controller"))
                .paths(PathSelectors.regex("/api.*"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .pathMapping("/");
    }
    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "Authorization", "header"));
    }
    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("/api.*"))
                        .build()
        );
    }
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }
    @Bean
    public Docket publicDocument() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("public")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.cltech.controller"))
                .paths(PathSelectors.regex("/public.*"))
                .build().pathMapping("/");
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题")
                .description("描述：模块的接口" +
                        " \n1、设置关联关系的所有表支持两级查询，只需要在request中传入相应的参数，目前操作两级以上的使用原生SQL(@SmartQuery注解)或者如下：\n" +
                        " \nSpecification<SysPermission> specification  = Specifications.<SysPermission>and()\n" +
                        " \n               .eq(!StringUtils.isEmpty(sysPermissionType),\"type\",sysPermissionType)\n" +
                        " \n               .eq(null != sysPermissionlevel,\"level\",sysPermissionlevel)\n" +
                        " \n               .predicate(!StringUtils.isEmpty(userId), ((root, query, cb) -> {\n" +
                        " \n                   Join roles = root.join(\"roles\", JoinType.INNER);\n" +
                        " \n                   Join users = roles.join(\"users\",JoinType.INNER);\n" +
                        " \n                   return cb.equal(users.get(\"id\"), userId);\n" +
                        " \n               })).build();\n"
                        +" \n2、两级以内的查询不在提供额外的接口，比如要查询某个组织机构下的用户（分页），直接调用用户分页查询，\n" +
                        " \n只是参数为organization.id=?"

                )
                .license("License Version 1.0")
                .version("1.0").build();
    }
}
